<?php 
namespace lib\protocol;
class WxMac{
    protected $api;
    protected $Header = [];
    public function __construct($api = null){
        $this->api = empty($api) ? 'http://pc.wfkc.tech:999':$api;
        $this->Header = ['Content-Type: application/json-patch+json'];
    }
    //创建微信
    public  function wxlogin(){
        $url= $this->api."/api/Client/WXCreate";
        $data=array(
              "Terminal"=>3,
              "WxData"=> "data",
              "Brand"=> "mac",
              "Name"=> "mac-tegic",
              "Imei"=> "string",
              "Mac"=> "mac"
        );
        $res=xfhttp_post_yun($url,$this->Header,$data);
        $res=json_decode($res,true);
        if($res['data']['Guid']){
            $guid = $res['data']['Guid'];
            $qrcode=$this->api."/api/Login/WXGetLoginQrcode";
            $data = array('Guid'=>$guid);
            $res=json_decode(xfhttp_post_yun($qrcode,$this->Header,$data),true);
            if($res['data']['qrcode']){
                return ["uuid" => $res["data"]["uuid"], "id" => strval($guid), "qr_url" =>"data:image/jpg;base64,". $res["data"]["qrcode"]];
            }
        }
        return ['code'=>-1,'msg'=>'未知错误'];
    }
    //二维码状态
    public  function wxCheckLoginQrcode($guid, $uuid){
        $WXCheck = $this->api."/api/Login/WXCheckLoginQrcode";
        $post = array(
    	    'Guid'=>$guid,
    	    'Uuid'=>$uuid
    	 );
    	$res = json_decode(xfhttp_post_yun($WXCheck, $this->Header,$post), true);
    	if ($res["data"]["state"] == 0) {
    	    $result = ["code" => 1, "msg" => "等待扫码中", "state" => $res["data"]["state"]];
    	}else if ($res["data"]["state"] == 1) {
    	    $result = ["code" => 2, "msg" => "等待确认中", "state" => $res["data"]["state"]];
    	}else if($res["data"]["state"] == 4){
    	    $result = ["code" => -1, "msg" => "已取消", "state" => $res["data"]["state"]];
    	}else if ($res["data"]["state"] == 2){
    	    $WXLogin = $this->api. "/api/Login/WXSecLoginManual";
    	    $pass = $res["data"]["wxnewpass"];
    		$wxid = $res["data"]["wxid"];
    		$post = array(
        		    "Guid"=>$guid,
        		    "Channel"=>1,
        		    "UserName"=>$wxid,
        		    "Password"=>$pass
    		 );
    		$res2 = json_decode(xfhttp_post_yun($WXLogin,$this->Header,$post), true);
    		$result = ["code" => 0, "msg" => "登录成功", "cookie" => $guid];
    	}
    	return $result;
    }
    //心跳状态
    public  function WXHeartBeat($guid){
        $api= $this->api."/api/Common/WXHeartBeat";
        $post = array(
    	    'Guid'=>$guid
    	 );
    	$res = json_decode(xfhttp_post_yun($api, $this->Header,$post), true);
    	if($res['data']){
    	 	 $result = ["code" => 1, "msg" => "心跳正常"];
	 	}else{
	 	     $result = ["code" => -1, "msg" => "账户已经离线"];
	 	}
	 	return $result;
    }
    public function Order($guid){
        $api=$this->api."/api/Message/WXSyncMsg";
        $post = array("Guid"=>$guid);
        $res = xfhttp_post_yun($api, $this->Header,$post);
        $result=$this->stripslashes_deep($res);
        return $result;
    }
    
    public function WXF2FQrcode($guid , $fee , $Description){
        $api = $this->api."/api/Payment/WXTransferSetF2FFee";
        $post = array(
	        'Fee'=>$fee*100,
	        'Description'=>$Description,
    	    'Guid'=>$guid
    	);
    	$res = json_decode(xfhttp_post_yun($api, $this->Header,$post), true);    
    	$arrs = $this->stripslashes_deep($res["data"]["reqText"]["buffer"]);
    	return $arrs;
    }
    protected function stripslashes_deep($value) {
        
        $value = is_array($value) ?
            array_map([$this, 'stripslashes_deep'], $value) : // 使用 $this 引用类方法
            stripslashes($value);
        return $value;
    }
}
?>